package com.geek.javaCource.demo.provider.mapper;

import com.geek.javaCource.demo.provider.entity.FrozenAccount;
import com.geek.javaCource.demo.provider.entity.Trace;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Create by chenhd on 2021/10/10.
 */
@Mapper
@Repository
public interface AccountMapper {

    /**
     * 账户表更新操作
     *
     * @param name
     * @param cnyCount
     * @param usCount
     * @return
     */
    @Update("update t_account set cny_count = cny_count + #{cny_count} ,us_count = us_count + #{us_count}  " +
            "where cny_count > -#{cny_count} and us_count > -#{us_count} and user_name = #{user_name}")
    int updateBalance(@Param("user_name") String name, @Param("cny_count") Double cnyCount, @Param("us_count") Double usCount);

    /**
     * 冻结表更新操作
     *
     * @param name
     * @param cnyCount
     * @param usCount
     * @return
     */
    @Insert("insert into t_frozen(user_name, cny_count, us_count, trace_id) values(#{user_name},#{cny_count},#{us_count},#{trace_id})")
    int insertFrozen(@Param("user_name") String name, @Param("cny_count") Double cnyCount, @Param("us_count") Double usCount, @Param("trace_id") String traceId);

    @Select("select * from t_frozen where trace_id = #{trace_id}")
    List<FrozenAccount> queryFrozen(@Param("trace_id") String trace_id);

    @Delete("delete from t_frozen where trace_id = #{trace_id}")
    int deleteFrozen(@Param("trace_id") String trace_id);

    @Update("update account_info set  frozen_balance = 0 " +
            "where frozen_balance > 0")
    int confirm();

    @Update("update t_account set account_balance = account_balance + frozen_balance , frozen_balance = 0 " +
            "where frozen_balance > 0")
    int cancel();

    @Select("select * from t_account where user_name = #{user_name}")
    List<Trace> queryOne(Trace trace);
}
